//
//  LoginViewController.m
//  绿地管理系统
//
//  Created by rimi on 16/1/21.
//  Copyright © 2016年 hexs. All rights reserved.
//

#import "LoginViewController.h"
#import "NetWorkingManager+Public.h"

@interface LoginViewController () <UITextFieldDelegate>

@property (nonatomic, strong) UIImageView   *backView;
@property (nonatomic, strong) UIImageView   *loginView;
@property (nonatomic, strong) UITextField   *usernameTextField;
@property (nonatomic, strong) UITextField   *passwordTextField;
//@property (nonatomic, strong) UIImageView   *passwordView;
//@property (nonatomic, strong) UILabel       *label;
@property (nonatomic, strong) UIButton      *loginButton;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initializeAppearance];
}

- (void)viewDidAppear:(BOOL)animated
{
    [self loginViewFlyToCenter];
}

- (void)initializeAppearance
{
    [self.view addSubview:self.backView];
    [self.view addSubview:self.loginView];
}

#pragma mark -- <UITextFieldDelegate>
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

- (void)onLoginButton:(UIButton *)sender
{
    [NetWorkingManager loginWithUsername:self.usernameTextField.text password:self.passwordTextField.text successHandler:^(id responseObj) {
//        NSLog(@"%@",responseObj);
        if ([responseObj[@"status"] integerValue] == 1) {
            [[NSUserDefaults standardUserDefaults] setObject:@(YES) forKey:@"AutoLogin"];
            [[NSUserDefaults standardUserDefaults] setObject:self.usernameTextField.text forKey:@"Username"];
            [[NSUserDefaults standardUserDefaults] setObject:self.passwordTextField.text forKey:@"Password"];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"LoginSuccess" object:nil];
            [self loginViewFlyOutToCenter];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self dismissViewControllerAnimated:NO completion:nil];
            });
        } else {
            NSString *errorMsg = responseObj[@"content"];
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:errorMsg preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:alert animated:YES completion:nil];
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [alert dismissViewControllerAnimated:YES completion:nil];
            });
        }
    } failureHandler:^(NSError *error) {
//        NSLog(@"%@",error);
//        NSLog(@"%@",self.usernameTextField.text);
//        NSLog(@"%@",self.passwordTextField.text);
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"网络请求失败" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alert animated:YES completion:nil];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [alert dismissViewControllerAnimated:YES completion:nil];
        });
    }];
}

- (void)loginViewFlyToCenter
{
    [UIView animateWithDuration:1.0 animations:^{
        _loginView.frame = CGRectMake(60, 200, 250, 185);
    } completion:^(BOOL finished) {
    }];
}
- (void)loginViewFlyOutToCenter
{
    [UIView animateWithDuration:1.0 animations:^{
        _loginView.frame = CGRectMake(60, -200, 250, 185);
    } completion:^(BOOL finished) {
    }];
}

#pragma mark -- getter
- (UIImageView *)backView
{
    if (_backView == nil) {
        _backView = [[UIImageView alloc] initWithFrame:self.view.bounds];
        _backView.image = [UIImage imageNamed:@"启动页面"];
    }
    return _backView;
}

- (UIImageView *)loginView
{
    if (_loginView == nil) {
        _loginView = [[UIImageView alloc] initWithFrame:CGRectMake(60, -200, 250, 185)];
        _loginView.image = [UIImage imageNamed:@"登录框_03"];
        _loginView.userInteractionEnabled = YES;
        
        _usernameTextField = [[UITextField alloc] initWithFrame:CGRectMake(55, 44, 195, 25)];
        _usernameTextField.placeholder = @"请输入用户名";
        _usernameTextField.textColor = [UIColor colorWithRed:117/255.0 green:171/255.0 blue:33/255.0 alpha:1.0];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(85, 240, 25, 25)];
        imageView.image = [UIImage imageNamed:@"登录_03"];
        _usernameTextField.leftView = imageView;
        _usernameTextField.delegate = self;
        _usernameTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
        _usernameTextField.autocorrectionType = UITextAutocorrectionTypeNo;
        _usernameTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
        [_loginView addSubview:_usernameTextField];
        
        _passwordTextField = [[UITextField alloc] initWithFrame:CGRectMake(55, 82, 195, 25)];
        _passwordTextField.placeholder = @"请输入密码";
        _passwordTextField.textColor = [UIColor colorWithRed:117/255.0 green:171/255.0 blue:33/255.0 alpha:1.0];
        UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(85, 280, 25, 25)];
        imageView.image = [UIImage imageNamed:@"登录_03"];
        _passwordTextField.leftView = imageView1;
        _passwordTextField.delegate = self;
        _passwordTextField.secureTextEntry = YES;
        _passwordTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
        _passwordTextField.autocorrectionType = UITextAutocorrectionTypeNo;
        _passwordTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
        [_loginView addSubview:_passwordTextField];
        
        _loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _loginButton.frame = CGRectMake(85, 345, 195, 30);
        [_loginButton setImage:[UIImage imageNamed:@"登录_03"] forState:UIControlStateNormal];
        _loginButton.center = CGPointMake(CGRectGetMidX(_loginView.bounds), CGRectGetMidY(_loginView.bounds)+55);
        [_loginButton addTarget:self action:@selector(onLoginButton:) forControlEvents:UIControlEventTouchUpInside];
        [_loginView addSubview:_loginButton];
    }
    return _loginView;
}

@end














